﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Entity;
using System.Data;

namespace Core.Dao
{
    class TicketCommentDao
    {
        public bool insert(TicketComment ticketComment)
        {
            object[,] param = {
                {"@TicketId", ticketComment.TicketId},
                {"@Content", ticketComment.Content},
                {"@ParentId", ticketComment.ParentId},
                {"@ParentType", ticketComment.ParentType},
                {"@Attachment1", ticketComment.Attachment1},
                {"@Attachment2", ticketComment.Attachment2},
                {"@Attachment3", ticketComment.Attachment3},
            };
            String sql = "insert into TICKET_COMMENTS(TKT_ID, TCM_CONTENT, TCM_TIMESTAMP, PARENT_ID, PARENT_TYPE, TCM_ATTACHMENT1, TCM_ATTACHMENT2, TCM_ATTACHMENT3) " +
                "values(@TicketId, @Content, GETDATE(), @ParentId, @ParentType, @Attachment1, @Attachment2, @Attachment3)";
            return DB.exec(sql, param);
        }

        public bool update(TicketComment ticketComment)
        {
            object[,] param = {
                {"@TicketId", ticketComment.TicketId},
                {"@Content", ticketComment.Content},
                {"@TimeStamp", ticketComment.TimeStamp},
                {"@ParentId", ticketComment.ParentId},
                {"@ParentType", ticketComment.ParentType},
                {"@Attachment1", ticketComment.Attachment1},
                {"@Attachment2", ticketComment.Attachment2},
                {"@Attachment3", ticketComment.Attachment3},
                {"@id", ticketComment.Id},
            };
            String sql = "update TICKET_COMMENTS set TKT_ID = @TicketId, TCM_CONTENT = @Content, TCM_TIMESTAMP = @TimeStamp, PARENT_ID = @ParentId, PARENT_TYPE = @ParentType " +
                "TCM_ATTACHMENT3 = @Attachment3, TCM_ATTACHMENT3 = @Attachment3, TCM_ATTACHMENT3 = @Attachment3 " +
                "where TCM_ID = @id";
            return DB.exec(sql, param);
        }

        public bool delete(int id)
        {
            object[,] param = {
                {"@id", id}   
            };
            String sql = "delete from TICKET_COMMENTS where TCM_ID = @id";
            return DB.exec(sql, param);
        }

        public bool delete(String ids)
        {
            object[,] param = {
                {"@ids", ids}   
            };
            String sql = "delete from TICKET_COMMENTS where TCM_ID in (" + ids + ")";
            return DB.exec(sql);
        }

        public DataTable get(int id)
        {
            object[,] param = {
                {"@id", id}   
            };
            String sql = "select * from TICKET_COMMENTS where TCM_ID = @id";
            return DB.getData(sql, param);
        }

        public DataTable getAll(int ticketId)
        {
            object[,] param = {
                {"@TicketId", ticketId}   
            };
            String sql = "select * from TICKET_COMMENTS where TKT_ID = @TicketId order by TCM_TIMESTAMP";
            return DB.getData(sql, param);
        }

        public bool deleteByTicket(int ticketId)
        {
            object[,] param = {
                {"@TicketId", ticketId}   
            };
            String sql = "delete from TICKET_COMMENTS where TKT_ID = @TicketId";
            return DB.exec(sql, param);
        }
    }
}
